package com.jinwei.qunguanjia.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.jinwei.qunguanjia.common.constant.UserConstants;
import com.jinwei.qunguanjia.common.core.domain.model.Condition;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.utils.*;
import com.jinwei.qunguanjia.user.dao.BlacklistDAO;
import com.jinwei.qunguanjia.user.dto.BlacklistDTO;
import com.jinwei.qunguanjia.user.dto.GroupDTO;
import com.jinwei.qunguanjia.user.po.BlacklistPO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("blacklistService")
public class BlacklistServiceImpl implements BlacklistService {

    @Resource
    private BlacklistDAO blacklistDAO;

    @Override
    public String save(BlacklistDTO blacklistDTO) {
        BlacklistPO blacklistPO = new BlacklistPO();
        BeanUtils.copyProperties(blacklistDTO, blacklistPO);
        String blacklistId = IdUtils.getWorkerId();
        blacklistPO.setBlacklistId(blacklistId);
        blacklistPO.setCreateTime(DateUtils.getDateTime());
        blacklistDAO.insert(blacklistPO);
        return blacklistId;
    }

    @Override
    public void remove(String id) {
        blacklistDAO.deleteById(id);
    }

    @Override
    public void update(BlacklistDTO blacklistDTO) {
        BlacklistPO blacklistPO = new BlacklistPO();
        BeanUtils.copyProperties(blacklistDTO, blacklistPO);
        blacklistDAO.updateById(blacklistPO);
    }

    @Override
    public BlacklistDTO get(String id) {
        BlacklistPO blacklistPO = blacklistDAO.selectById(id);
        if (null == blacklistPO) {
            return null;
        }
        BlacklistDTO blacklistDTO = new BlacklistDTO();
        BeanUtils.copyProperties(blacklistPO, blacklistDTO);
        return blacklistDTO;
    }

    @Override
    public BlacklistDTO get(BlacklistDTO blacklistDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(blacklistDTO);
        // 拼接查询条件
        QueryWrapper<BlacklistPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        BlacklistPO blacklistPO = blacklistDAO.selectOne(queryWrapper);
        if (null == blacklistPO) {
            return null;
        }
        BeanUtils.copyProperties(blacklistPO, blacklistDTO);
        return blacklistDTO;
    }

    @Override
    public Long count(BlacklistDTO blacklistDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(blacklistDTO);
        // 拼接查询条件
        QueryWrapper<BlacklistPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return blacklistDAO.selectCount(queryWrapper);
    }

    @Override
    public List<BlacklistDTO> list(BlacklistDTO blacklistDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(blacklistDTO);
        // 拼接查询条件
        QueryWrapper<BlacklistPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return JsonUtils.copyList(blacklistDAO.selectList(queryWrapper), BlacklistDTO.class);
    }

    @Override
    public PageDataInfo<BlacklistDTO> listPage(BlacklistDTO blacklistDTO) {
        List<BlacklistDTO> list = blacklistDAO.list(blacklistDTO);
        PageInfo<BlacklistDTO> pageInfo = new PageInfo<>(list);
        PageDataInfo<BlacklistDTO> pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setData(list);
        pageDataInfo.setTotal(pageInfo.getTotal());
        pageDataInfo.setTotalPage(PageCountUtils.getPageCount(pageInfo.getTotal(), pageInfo.getPageSize()));
        return pageDataInfo;
    }

    @Override
    public String checkUnique(BlacklistDTO blacklistDTO) throws IllegalAccessException {
        String id = StringUtils.isNull(blacklistDTO.getBlacklistId()) ? "-1" : blacklistDTO.getBlacklistId();
        BlacklistDTO info = get(blacklistDTO);
        if (StringUtils.isNotNull(info) && !info.getBlacklistId().equals(id)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
    
}
